export const baseUrl = "https://www.ymzctech.com"
export default function request(options) {
  const url = baseUrl + options.url
  const method = options.method || "Get"
  const data = options.data || {}

  // 添加请求头
  const header = {
    // 'content-type': 'application/json',
    'x-access-token': wx.getStorageSync('token') || ''
  }

  // 返回Promise
  return new Promise((resolve,reject)=> {
    wx.request({
      url,
      method,
      data,
      header,
      success(res) {
        const type = Object.prototype.toString.call(res.data)
        const {code, data, msg} = res.data
        resolve(res.data)
        // if(code === 0) {
        //   resolve(res.data)
        // } else if(code == 400) {
        //   wx.navigateTo({
        //     url: '/pages/login/login',
        //   })
        //   reject(msg || '请求失败')
        // }
      },
      fail(err) {
        wx.showToast({
          title: '网络错误，请稍后再试',
          icon: 'none'
        })
        reject(err)
      }
    })
  })
}